package com.example.mybatisplus.controller;

import com.example.starterdemo.BambooServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.*;

/**
 * @Description:
 * @Author: dengping.xu
 * @Data: 2021-12-01
 */
@RestController
public class DemoController {

    private static ExecutorService executorService = Executors.newFixedThreadPool(10);

    @Autowired
    private BambooServer bmbooService;

    private String name;


    @GetMapping("timeout")
    public String timeout(){
        Future<String> submit = executorService.submit(() -> {
            try {
                Thread.sleep(10 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                return "execption";
            }
            return "hello";
        });
        String result;
        try {
            result = submit.get(8, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return "InterruptedException";
        } catch (ExecutionException e) {
            e.printStackTrace();
            return "ExecutionException";
        } catch (TimeoutException e) {
            e.printStackTrace();
            return "TimeoutException";
        }
        return result;
    }

    @RequestMapping("/")
    public Object index(){
        return "helll demo"+bmbooService.getName()+ LocalDate.now();
    }

    @RequestMapping("/hello")
    public Object hello(){
        return "helll " + name;
    }

    @RequestMapping("/setName")
    public Object hello(String setName){
        name = setName;
        return "set " + name;
    }


    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            System.out.println("hello");
        });
        thread.start();
        Thread.sleep(1000);
        thread.start();
    }

}
